package com.admore.demo;

import static com.admore.sdk.config.AdMoreConstants.AD_APP_ID;
import static com.admore.sdk.config.AdMoreConstants.AD_HEIGHT;
import static com.admore.sdk.config.AdMoreConstants.AD_SPLASH_ADN_ID;
import static com.admore.sdk.config.AdMoreConstants.AD_WIDTH;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.admore.demo.util.UIUtils;
import com.admore.sdk.ads.AdMoreSplashAd;
import com.admore.sdk.callback.AdMoreLoadListener;
import com.admore.sdk.callback.AdMoreMediaListener;
import com.admore.sdk.sdk.AdMoreSdk;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zcy.base.ui.BaseActivity;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import io.reactivex.functions.Consumer;


/**
 * Created by zcy on 2019/6/13.
 *
 * @author zhaochenyu@mxtrip.cn
 * @Date 2019/6/13.
 */
public class WelcomeActivity extends BaseActivity {

    private RxPermissions rxPermissions;
    private FrameLayout adMore;
    private ImageView iv;
    private static final int HANDLER_CODE_AD =100;
    private static final int HANDLER_CODE_NEXT = 101;
    private WelcomeHandler mHandler;
    private AdMoreSplashAd splashAd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setBackgroundDrawable(null);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        adMore = findViewById(R.id.ad_main);
        iv = findViewById(R.id.iv);
    }

    @Override
    public void initData() {
        mHandler=new WelcomeHandler(this);
        rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE).subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) throws Exception {
                if (permission.granted) {
                    checkAd();
                } else if (permission.shouldShowRequestPermissionRationale) {

                } else {

                }
                //3秒后如果没有加载广告就跳过
                mHandler.sendEmptyMessageDelayed(HANDLER_CODE_NEXT,6000);
            }
        });

    }

    private void gotoNext() {
        startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
    }

    private static class WelcomeHandler extends Handler{
        private SoftReference<WelcomeActivity> softActivity;

        public WelcomeHandler(WelcomeActivity activity) {
            this.softActivity = new SoftReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(softActivity.get()!=null){
                switch (msg.what){
                    case HANDLER_CODE_AD:
                        softActivity.get().checkAd();
                        break;
                    case HANDLER_CODE_NEXT:
                        softActivity.get().gotoNext();
                        break;
                }
            }
        }
    }

    private void checkAd() {
        if(AdMoreSdk.init){
            Log.d(TAG,"广告初始化成功，开始加载广告");
            loadSplash();
            mHandler.removeMessages(HANDLER_CODE_AD);
        } else {
            mHandler.sendEmptyMessageDelayed(HANDLER_CODE_AD,100);
        }
    }

    private void loadSplash() {
        if (!AdMoreSdk.init) {
            return;
        }
        HashMap<String, Object> options = new HashMap<>();
        options.put(AD_WIDTH, UIUtils.getScreenWidth(getApplicationContext()));
        options.put(AD_HEIGHT, UIUtils.getScreenHeight(getApplicationContext()));
        options.put(AD_SPLASH_ADN_ID, App.AD_MORE_SPLASH_ADN_ID);
        options.put(AD_APP_ID, App.AD_MORE_APP_ID);
        splashAd = AdMoreSdk.getAdMoreSdkManager().createSplash(this, App.AD_MORE_SPLASH_ID, options);
        splashAd.setLoadListener(new AdMoreLoadListener() {
            @Override
            public void onAdLoadSuccess(String msg) {
                Log.d(TAG, "onAdLoadSuccess");
                showSplash();
                mHandler.removeMessages(HANDLER_CODE_NEXT);
            }

            @Override
            public void onAdLoadError(Throwable throwable) {
                Log.d(TAG, "onAdLoadError" + throwable);

            }
        });
        splashAd.loadAd(3000);

    }

    private void showSplash() {
        if (!splashAd.isReady()) {
            return;
        }
        splashAd.setMediaListener(new AdMoreMediaListener() {
            @Override
            public void onAdClosed() {
                Log.d(TAG, "onAdClosed");
                mHandler.sendEmptyMessage(HANDLER_CODE_NEXT);
            }

            @Override
            public void onAdShow() {
                Log.d(TAG, "onAdShow ecpm:" + splashAd.getAdMoreInfo().getECPM() + " name:" + splashAd.getAdMoreInfo().getSdkName());
            }
        });
        splashAd.show(true, adMore);
    }
}
